// pages/aibo/pay/pay.js

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {

    payItem:[
      {number:20,
      check:false},
      {number:50,
        check:false},{number:100,
          check:false},{number:300,
            check:false},{number:500,
              check:false},{number:1000,
                check:false}
    ],
    content:'',
    payNum:0,
    totlePay:0,
    aiboid:'',
    openID:""


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    console.log(options)
    
    this.data.openID = app.globalData.OpenID
    console.log(this.data.openID)
    this.getscene(options)
  },
  getscene(options) {
      if (options.scene != undefined) {
        const scene = decodeURIComponent(options.scene)

        var keyValue = scene.split('=');

        // 创建一个对象，并以键值对的方式赋值
        var obj = {};
        obj[keyValue[0]] = keyValue[1];
        console.log("scene"+ obj.webID)
        app.aibo.webID = obj.webID
        this.data.aiboid = obj.webID; // 获取属性值
      }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

//获取小程序openID，和web过来的aibo di
if (app.globalData.OpenID == "" || app.globalData.OpenID == undefined) {
  wx.showModal({
    title: '',
    content: '请您登录验证',
    confirmText: '登录',
    complete: (res) => {
      if (res.cancel) {
        setTimeout(()=>{
          wx.navigateTo({
            url: '/pages/login/atLogin/atLogin?aiboType='+'pay',
          })
        },700)
      
      }
      if (res.confirm) {
        setTimeout(()=>{
          wx.navigateTo({
            url: '/pages/login/atLogin/atLogin?aiboType='+'pay',
          })
        },700)
      }
    }
  })
  return
}
  },

  
  selectPayItem(data){
    console.log(data)
    var listPay = this.data.payItem
    for(var i=0;i<listPay.length;i++){
      if(i == data.currentTarget.dataset.index){
        listPay[i].check = true
      }else{
        listPay[i].check = false
      }
    }
    this.setData({
      payItem:listPay,
      payNum:data.currentTarget.dataset.item.number,
      totlePay:data.currentTarget.dataset.item.number/10
    }) 
  },
  payAibo(){

    
   
    var that = this
    var aiboID = that.data.aiboid
    this.data.openID = app.globalData.OpenID
    if(that.data.totlePay == 0){
      wx.showToast({
        title: '请选择充值类型',
        icon:'none'
      })
      return
    }
    
    wx.showLoading({
      title: '支付中',
    })
  
    console.log(aiboID)
    console.log( this.data.openID)
    console.log(that.data.totlePay)
   
   
    wx.request({
      url: app.app_url.BASE_URL+app.app_url.AIBO_PAY,
      method:'POST',
      data:{
        'mobile':this.data.aiboid,
        'openid':that.data.openID,
        'payAmount':that.data.totlePay*100
      },
      success(res){
       wx.hideLoading()
       console.log(res)
       if(res.statusCode == 200){
         if(res.data.errorcode == 0){ wx.requestPayment({
          nonceStr: res.data.data.nonceStr,
          package: res.data.data.package,
          paySign: res.data.data.paySign,
          timeStamp: res.data.data.timeStamp,
          signType: 'RSA',
          success(res){
            if(res.errMsg == "requestPayment:ok"){
              wx.showModal({
                title: '谢主公',
                content: '在下愿为主公效犬马之劳',
                complete: (res) => {
                  if (res.cancel) {
                    
                  }
              
                  if (res.confirm) {
                     wx.navigateBack()
                  }
                }
              })
            }
          },fail(res){
           console.log(res)
          }
        })
      }else
      {
         
          wx.showToast({
            title: '支付失败',
          })
        }
      
      }else{
        console.log(res)
      wx.showToast({
        title: '支付失败',
      })
      }
        
      },fail(res){
        wx.hideLoading()
        console.log(res)
      }
    })
  },
   removeQuotes(str) {
    return str.replace(/^"|"$/g, '');


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {


  }
})